package org.bedracket.bedracketenchantment.enchantment;

import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentCategory;

import java.util.Random;

public class MergeEffectEnchantment extends Enchantment {
    public MergeEffectEnchantment() {
        super(Rarity.VERY_RARE, EnchantmentCategory.WEAPON, new EquipmentSlot[]{EquipmentSlot.MAINHAND});
    }

    @Override
    public int getMaxLevel() {
        return 3;
    }

    @Override
    public int getMinCost(int level) {
        return 1 + (level - 1) * 11;
    }

    @Override
    public void doPostAttack(LivingEntity pAttacker, Entity pTarget, int pLevel) {
        if (pTarget instanceof final LivingEntity living) {
            if (living.getActiveEffectsMap().containsKey(MobEffects.REGENERATION)) {
                int count = living.getActiveEffects().size() / 2;
                living.removeAllEffects();
                Random random = new Random();
                int randomPid = random.nextInt(1, 31);
                for (int i=1; i<= count; i++) {
                    living.addEffect(new MobEffectInstance(MobEffect.byId(randomPid), 5 * 20 * pLevel, pLevel));
                    living.hurt(DamageSource.CRAMMING, 2 * pLevel);
                }
            }
        }
    }
    @Override
    protected boolean checkCompatibility(Enchantment pOther) {
        return super.checkCompatibility(pOther) && !(pOther instanceof SplitEffectEnchantment);
    }
}
